package com.gxa.service.shezhiservice.Impl;

import com.gxa.common.R;
import com.gxa.mapper.shezhimapper.ZhiYuanMapper;
import com.gxa.pojo.ZhiYuan;
import com.gxa.service.shezhiservice.ZhiYuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author weishen
 */
@Service
public class ZhiYuanServiceImpl implements ZhiYuanService {

    @Autowired
    ZhiYuanMapper zhiYuanMapper;

    @Override
    public R selectAllZhiYuanForPage(int page, int size) {
        R r= new R();
        int index = (page-1)*size;
        System.out.println("这里的index是："+index);
        List<ZhiYuan> zhiyuans = zhiYuanMapper.selectAllZhiYuanForPage(index, size);
        r.setCode(200);
        r.setMsg("查询成功！");
        r.setCount(selectCountZhiYuan());
        r.setData(zhiyuans);
        return r;
    }

    @Override
    public int selectCountZhiYuan() {
        return zhiYuanMapper.selectCountZhiYuan();
    }

    @Override
    public R updateZhiYuanById(ZhiYuan zhiYuan) {
        R r = new R();
        int res = zhiYuanMapper.updateZhiYuanById(zhiYuan);
        r.setCode(500);
        r.setMsg("更新失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("更新成功");
        }
        return r;
    }

    @Override
    public R insertZhiYuan(ZhiYuan zhiYuan) {
        R r = new R();
        int res = zhiYuanMapper.insertZhiYuan(zhiYuan);
        r.setCode(500);
        r.setMsg("新增失败");
        if(res>0)
        {
            r.setCode(200);
            r.setMsg("新增成功");
        }
        return r;
    }

    @Override
    public R deleteZhiYuanById(ArrayList<Integer> id) {
        R r = new R();
        int res=0;
        int flag=1;
        for(int i=0;i<id.size();i++)
        {
            res=zhiYuanMapper.deleteZhiYuanById(id.get(i));
            if(res==0)
            {
                flag=0;
                break;
            }
        }
        r.setCode(200);
        r.setMsg("删除成功");
        if(flag==0)
        {
            r.setCode(500);
            r.setMsg("删除失败");
        }
        return r;
    }
}
